package top.zhimingw.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import top.zhimingw.pojo.Kind;
import top.zhimingw.redis.KindKey;
import top.zhimingw.service.BaseService;
import top.zhimingw.service.KindService;

import java.util.List;

/**
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/5/31 21:54
 */
@Service("kindService")
public class KindServiceImpl extends BaseService implements KindService {
    @Override
    public boolean insert(Kind kind) {
        if(kindMapper.insert(kind)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
        if(kindMapper.deleteById(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public Kind queryById(Integer id) {
        Kind kind = null;
        if(redisService.exists(KindKey.getById,String.valueOf(id))){
            kind = redisService.get(KindKey.getById,String.valueOf(id),Kind.class);
        }else{
            kind = kindMapper.queryById(id);
            if(!ObjectUtils.isEmpty(kind)){
                redisService.set(KindKey.getById,String.valueOf(id),kind,60*60*24);
            }
        }
        return kind;
    }

    /**
    * @Description: 查询全部数据
    * @Param: []
    * @return: java.util.List<top.zhimingw.pojo.Kind>
    * @Author: zhimingw
    * @Date: 2021/5/31
    */
    @Override
    public List<Kind> queryAll() {
        List<Kind> kinds = null;
        if(redisService.exists(KindKey.getIndex,"")){
            kinds = redisService.getList(KindKey.getIndex,"",Kind.class);
        }else{
            kinds = kindMapper.queryAll();
            kinds.forEach(kind -> {
                kind.setArticleCount(articleKindMapper.getArticleCount(kind.getId()));
            });
        }
        return kinds;
    }

    /**
    * @Description: 实体作为筛选条件查询数据
    * @Param: [kind]
    * @return: java.util.List<top.zhimingw.pojo.Kind>
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @Override
    public List<Kind> queryAll(Kind kind) {
        return kindMapper.queryAll(kind);
    }

    /**
    * @Description: 修改数据，哪个属性不为空就修改哪个属性
    * @Param: [kind]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @Override
    public boolean update(Kind kind) {
        if(kindMapper.update(kind)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 根据文章ID查询所有分类
    * @Param: [id]
    * @return: top.zhimingw.pojo.Kind
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @Override
    public Kind queryByArticleId(Integer id) {
        return articleKindMapper.queryByArticleId(id);
    }


    /**
    * @Description: 根据文集ID获取关联文章的数量
    * @Param: [id]
    * @return: java.lang.Integer
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @Override
    public Integer getArticleCount(Integer id) {
        return articleKindMapper.getArticleCount(id);
    }

    @Override
    public Integer getCount() {
        return kindMapper.getCount();
    }
}
